package com.dylan.shot.head.controller;

import com.dylan.shot.common.config.SmsSettings;
import com.dylan.shot.common.config.WechatSettings;
import com.dylan.shot.common.model.City;
import com.dylan.shot.common.model.Province;
import com.dylan.shot.common.service.CityService;
import com.dylan.shot.common.service.ProvinceService;
import com.dylan.shot.head.vo.SimpleTip;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Dylan on 2017/9/4.
 */
@Controller
public class HomeController {
    private Logger LOG = LoggerFactory.getLogger(getClass());

    @Autowired
    private SmsSettings smsSettings;
    @Autowired
    private WechatSettings wechatSettings;
    @Autowired
    private ProvinceService provinceService;
    @Autowired
    private CityService cityService;
    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping({"", "/"})
    @ResponseBody
    public SimpleTip home(HttpSession session) {
        LOG.info("session ID : {}", session.getId());
        Object rand = session.getAttribute("rand");
        if (rand == null) {
            rand = RandomUtils.nextInt(10000, 99999);
            session.setAttribute("rand", rand);
            LOG.info("rand为空,重新生成: {}", rand);
        } else {
            LOG.info("rand不为空,值为: {}", rand);
        }
        List<City> list = cityService.getByCitySimpleNameLike("北");
        LOG.debug("查询城市个数:{}", list.size());
        Province beijing = provinceService.selectByKey("110000");
        beijing.setProvSimpleName(null);
        Map<String, Object> map = new HashMap<>();
        map.put("myID", 1231234);
        map.put("myName", "安康");
        map.put("myAddr", null);
        map.put("beijing", beijing);
        return SimpleTip.success("成功")
                .putInfo("myFeature", null)
                .putInfo("smsHost", smsSettings.getHost())
                .putInfo("smsPort", smsSettings.getPort())
                .putInfo("appId", wechatSettings.getAppId())
                .putInfo("appSecret", wechatSettings.getAppSecret())
                .putInfo("cities", list)
                .putInfo("province", beijing)
                .putInfo("date", new Date())
                .putInfo("map", map)
                .bindInfo();
    }

    @RequestMapping("/page")
    public String homePage(Model model) {
        model.addAttribute("username", "安康");
        model.addAttribute("date", new Date());
        model.addAttribute("price", 1123123.123123);
        return "page";
    }

}
